package com.xzj.common.file.excel.style;

import org.apache.poi.ss.usermodel.*;

/**
 * @program: xzj-common
 * @ClassName: CustomCellStyle
 * @description: 导出excel基本样式
 * @author: XiaZunJun
 * @create: 2024-08-26 09:01
 */
public class CustomCellStyle {
    private static final short FONT_SIZE_TEN = 10;
    private static final short FONT_SIZE_TWELVE = 12;

    public static CellStyle createHeaderCellStyle(Workbook workbook){
        CellStyle style = getBaseCellStyle(workbook);
        style.setFont(getFont(workbook, FONT_SIZE_TWELVE, true));
        return style;
    }

    /**
     * 初始化--数据行样式
     *
     * @param workbook 导出excel工作簿
     * @return CellStyle 样式
     */
    public static CellStyle createContentCellStyle(Workbook workbook) {
        CellStyle style = getBaseCellStyle(workbook);
        style.setFont(getFont(workbook, FONT_SIZE_TEN, false));
        return style;
    }

    /**
     * 初始化--基础样式
     *
     * @param workbook 导出excel工作簿
     * @return CellStyle 样式
     */
    private static CellStyle getBaseCellStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        //下边框
        style.setBorderBottom(BorderStyle.THIN);
        //左边框
        style.setBorderLeft(BorderStyle.THIN);
        //上边框
        style.setBorderTop(BorderStyle.THIN);
        //右边框
        style.setBorderRight(BorderStyle.THIN);
        //水平居中
        style.setAlignment(HorizontalAlignment.CENTER);
        //上下居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        //设置自动换行
        style.setWrapText(true);
        return style;
    }

    /**
     * 字体样式
     *
     * @param size   字体大小
     * @param isBold 是否加粗
     * @return Font 字体
     */
    private static Font getFont(Workbook workbook, short size, boolean isBold) {
        Font font = workbook.createFont();
        //字体样式
        font.setFontName("宋体");
        //是否加粗
        font.setBold(isBold);
        //字体大小
        font.setFontHeightInPoints(size);
        return font;
    }

    /**
     * 字体样式
     *
     * @param workbook 工作簿
     * @param isBold 是否加粗
     * @param color 字体颜色
     * @return Font 字体
     */
    public static Font defaultFont(Workbook workbook, boolean isBold,short color) {
        Font font = getFont(workbook, FONT_SIZE_TEN, isBold);
        font.setColor(color);
        return font;
    }
}
